package com.wolffy.flink.udf;

import com.wolffy.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;


public class MapRichDemo {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<WaterSensor> waterSensorDS = env.fromElements(
                new WaterSensor("s1", 1L, 1),
                new WaterSensor("s1", 2L, 2),
                new WaterSensor("s1", 3L, 3)
        );
        SingleOutputStreamOperator<String> waterSensorMapDS = waterSensorDS.map(new RichMapFunction<WaterSensor, String>() {
            @Override
            public String map(WaterSensor value) throws Exception {
                return value.getId();
            }

            @Override
            public void open(Configuration parameters) throws Exception {
                System.out.println("open-xxx");
            }

            @Override
            public void close() throws Exception {
                System.out.println("close-xxx");
            }
        });
        waterSensorMapDS.print();
        env.execute();
    }
}
